import 'package:flutter/material.dart';
import 'package:mobile_app.core/core/presentation/theme/theme.dart';

class FailureIndicator extends AnimatedWidget {
  const FailureIndicator({
    required AnimationController controller,
    super.key,
  }) : super(listenable: controller);

  AnimationController get _controller => listenable as AnimationController;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      height: double.infinity,
      child: Center(
        child: Opacity(
          opacity: 1.0 - _controller.value,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Container(
              color: CoreTheme.of(context).failureIndicatorBackgroundColor,
              width: 150,
              height: 150,
              child: CustomPaint(
                painter: _CrossPainter(
                  color: CoreTheme.of(context).failureIndicatorColor,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class _CrossPainter extends CustomPainter {
  const _CrossPainter({
    required this.color,
  });

  final Color color;

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = 6.5
      ..strokeCap = StrokeCap.round;

    final padding = 0.29 * size.width;

    canvas.drawLine(
      Offset(padding, padding),
      Offset(size.width - padding, size.height - padding),
      paint,
    );

    canvas.drawLine(
      Offset(size.width - padding, padding),
      Offset(padding, size.height - padding),
      paint,
    );
  }

  @override
  bool shouldRepaint(_CrossPainter oldDelegate) {
    return false;
  }
}
